package cn.piaoyi.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import org.apache.commons.logging.*;

import cn.piaoyi.bean.Address;
import cn.piaoyi.bean.UserDetail;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.dao.AddressDAO;
import cn.piaoyi.dao.UserDetailDAO;
import cn.piaoyi.dao.UserInfoDAO;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.UserInfoService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;


public class UserInfoServiceImp implements UserInfoService {

	private static final Log logger = LogFactory.getLog(UserInfoServiceImp.class);
	
	private UserInfoDAO userInfoDAO;
	private AddressDAO addressDAO;

	public UserInfoDAO getUserInfoDAO() {
		return userInfoDAO;
	}

	public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}
	
	public AddressDAO getAddressDAO() {
		return addressDAO;
	}

	public void setAddressDAO(AddressDAO addressDAO) {
		this.addressDAO = addressDAO;
	}

	/**
	 * 保存UserInfo对象
	 * 
	 * @param userInfo
	 * 			UserInfo对象
	 * @return UserInfo
	 * 			UserInfo对象
	 */
	public UserInfo createUserInfo(UserInfo userInfo,Address address) throws PiaoyiException{
		try {
			userInfo = this.getUserInfoDAO().saveUserInfo(userInfo);
			address.setUid(userInfo.getId());
			this.getAddressDAO().saveAddress(address);
			return userInfo;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}
	
	/**
	 * 根据account获取UserInfo对象
	 * 
	 * @param account
	 * 			String
	 * @return UserInfo
	 * 			UserInfo对象
	 */
	public UserInfo findUserInfoByAccount(String account){
		return this.getUserInfoDAO().findUserInfoByAccount(account);
	}
	
	/**
	 * 保存UserInfo对象
	 * 
	 * @param userInfo
	 * 			UserInfo对象
	 * @return UserInfo
	 * 			UserInfo对象
	 */
	public UserInfo updateUserInfo(UserInfo userInfo) throws PiaoyiException{
		try {
			userInfo = this.getUserInfoDAO().updateUserInfo(userInfo);
			return userInfo;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}
	
	/**
	 * 根据主键ID获取Admin对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return UserInfo
	 * 			UserInfo对象
	 */
	public UserInfo findUserInfoById(long id){
		return this.getUserInfoDAO().findUserInfoById(id);
	}
	
	/**
	 * 获取UserInfo列表
	 * 
	 * @param account
	 * 			String
	 * @param validated
	 * 			int
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param pages
	 * 			  Pages
	 * @return PageList
	 * 			UserInfo对象列表
	 */
	public PageList findUserInfos(String account,int validated,String orderby, int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getUserInfoDAO().getUserInfoNum(account,validated));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
		List l = this.getUserInfoDAO().findUserInfos(account,validated,orderby, ascOrDesc,pages.getSpage(), pages.getPerPageNum());
		if(l == null || l.isEmpty()){
	   		 pl.setObjectList(null);
	   	     pl.setPages(pages);
	   		 return pl;
		}else{
			pl.setObjectList(l);
	        pl.setPages(pages);
	        return pl;
		}
	}
	
	/**
	 * 删除UserInfo对象
	 */
	public void removeUserInfoById(long id){
		this.getUserInfoDAO().removeUserInfoById(id);
	}
	
}
